package review;

import java.util.Arrays;

/*
* 排序
*
* */
public class Sort {

    public static void main(String[] args) {
        int[] nums = {7,8,4,3,4,6,8,7,6,5,5};
       /* int[] sort = bubbleSort(nums);
        for (int i : sort) {
            System.out.println(i);
        }*/
       /* int[] sort = selectSort(nums);
        for (int i : sort) {
            System.out.println(i);
        }*/
        int[] ints = insertSort(nums);
        for (int anInt : ints) {
            System.out.println(anInt);
        }
    }

    public static int[] bubbleSort(int[] nums){
        int[] temp = Arrays.copyOf(nums,nums.length);

        int size = temp.length;
        int swap = 0;
        boolean flag = false;
        for (int i = 0; i < size - 1; i++) {

            for (int j = 0; j < size - i - 1; j++) {
                if (temp[j] > temp[j+1]){
                    swap = temp[j];
                    temp[j] = temp[j + 1];
                    temp[j + 1] = swap;
                    flag = true;
                }
            }
            if (flag){
                flag = false;
            }else {
                break;
            }
        }


        return temp;
    }

    public static int[] selectSort(int[] nums){
        int[] temp = Arrays.copyOf(nums,nums.length);
        int size = nums.length;
        int min ;
        int minIndex;
        for (int i = 0; i < size; i++) {
            minIndex = i;
            min = temp[i];
            for (int j = i; j < size; j++) {
                if (temp[j] < min){
                    min = temp[j];
                    minIndex = j;
                }
            }
            temp[minIndex] = temp[i];
            temp[i] = min;
        }
        return temp;

    }

    public static int[] insertSort(int[] nums){
        int[] temp  =Arrays.copyOf(nums,nums.length);
        int size = nums.length;

        for (int i = 1; i < size; i++) {
            int j = i;
            int swap = temp[i];
            while(j>0 && swap <temp[j-1]){
                temp[j] = temp[j-1];
                j--;
            }
            if (i!=j){
            temp[j] = swap;
            }
        }

        return temp;
    }

    public static int[] shellSort(int[] nums){
        int[] temp = Arrays.copyOf(nums,nums.length);


        return temp;
    }
}
